package dataCollection;

import java.util.Random;

import util.Messages;

public class Die {
  final private String name_;
  final private int sides_;
  private Random r_;
  private Die(int sides) {
    sides_ = sides;
    name_ = Messages.getString("Dice."+sides);
    r_ = new Random();
  }

  public static Die autoCreateDie(String value) throws InvalidDiceException {
    if(value.equals(Messages.getString("Dice."+2)))
      return createD2 ();
    else if(value.equals(Messages.getString("Dice."+4)))
      return createD4 ();
    else if(value.equals(Messages.getString("Dice."+6)))
      return createD6 ();
    else if(value.equals(Messages.getString("Dice."+8)))
      return createD8 ();
    else if(value.equals(Messages.getString("Dice."+10)))
      return createD10 ();
    else if(value.equals(Messages.getString("Dice."+12)))
      return createD12 ();
    else if(value.equals(Messages.getString("Dice."+20)))
      return createD20 ();
    throw new InvalidDiceException();
  }
  
  public static Die autoCreateDie(int value) throws InvalidDiceException {
    switch(value) {
    case 2 : return createD2 ();
    case 4 : return createD4 ();
    case 6 : return createD6 ();
    case 8 : return createD8 ();
    case 10: return createD10();
    case 12: return createD12();
    case 20: return createD20();
    default: throw new InvalidDiceException();
    }
  }
  
  public static Die createD2() {
    Die d2 = new Die(2);
    return d2;
  }
  
  public static Die createD4() {
    Die d4 = new Die(4);
    return d4;
  }
  
  public static Die createD6() {
    Die d6 = new Die(6);
    return d6;
  }
  
  public static Die createD8() {
    Die d8 = new Die(8);
    return d8;
  }
  
  public static Die createD10() {
    Die d10 = new Die(10);
    return d10;
  }
  
  public static Die createD12() {
    Die d12 = new Die(12);
    return d12;
  }
  
  public static Die createD20() {
    Die d20 = new Die(20);
    return d20;
  }
  
  public String toString() {
    return name_;
  }
  
  public int getSides() {
    return sides_;
  }
  
  public int roll() {
    return (r_.nextInt()%sides_) + 1;
  }
}
